package com.atguigu.tingshu.search.rpc;

import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.search.service.ItemService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

/**
 * ClassName: ESInfoRpcController
 * Package: com.atguigu.tingshu.search.rpc
 * Description:
 *
 * @Author 梅超凡
 * @Create 2025/2/6 20:19
 * @Version 1.0
 */
@Tag(name = "Search模块微服务远程调用")
@RestController
@RequestMapping("api/search/rpc/albumInfo")
public class ESInfoRpcController {


    @Autowired
    private ItemService itemService;


    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient;

    //TODO 手动上架，自己写接口自己调用
    //每次启动都调用这个接口
    @PostMapping("/PutOnAlbumEs")
    @Operation(summary = "自写接口调用上架接口载入Es数据库")
    public Result PutOnAlbumEs() {
        //1.查询所有albumId
        ArrayList<Long> albumIds = albumInfoFeignClient.getAllAlbumInfoIds().getData();
        albumIds.forEach(
                albumId -> {
                    //2.遍历所有albumId并远程调用上架专辑服务进行专辑上架
                    itemService.onAlbumEs(albumId);
                }
        );
        return Result.ok("success");
    }


    @PostMapping("/onAlbumEs/{albumId}")
    @Operation(summary = "上架专辑到es")
    public Result onAlbumEs(@PathVariable(value = "albumId") Long albumId) {
        if (albumId != 0L){
            itemService.onAlbumEs(albumId);
        }
        return Result.ok();
    }


    @PostMapping("/offAlbumEs/{albumId}")
    @Operation(summary = "下架专辑到es")
    public Result offAlbumEs(@PathVariable(value = "albumId") Long albumId) {
        if (albumId != 0L){
            itemService.offAlbumEs(albumId);
        }
        return Result.ok();
    }
}
